﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;

namespace Admin.Controllers
{
    public class UtilController : BaseController
    {
        //
        // GET: /Util/
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult KindEditorHandlerFile()
        {
            FileUpload.FileUpload upfile = new FileUpload.FileUpload();
            var extTable = new Hashtable
                               {
                                   {"image", "gif,jpg,jpeg,png,bmp"},
                                   {"flash", "swf,flv"},
                                   {"media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"},
                                   {"file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"}
                               };
            const int maxSize = 2000000;

            HttpPostedFileBase file = Request.Files["imgFile"];
            if (file == null)
            {
                return ShowError("请选择文件。");
            }
            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                return ShowError("上传文件大小超过限制。");
            }
            var dirName = Request.QueryString["dir"];
            if (String.IsNullOrEmpty(dirName))
            {
                dirName = "image";
            }
            var fileName = file.FileName;
            var extension = Path.GetExtension(fileName);
            if (extension == null)
            {
                return ShowError("extension == null");
            }
            var fileExt = extension.ToLower();
            if (String.IsNullOrEmpty(fileExt) ||
                Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                return ShowError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
            }

            var type = Request["type"];
            Stream stream = file.InputStream;
            byte[] b = StreamToBytes(stream);
            string img = upfile.UploadFilesByDocument(b, file.FileName);
            var hash = new Hashtable();
            hash["error"] = 0;
            hash["url"] = img;
            return Json(hash, "text/html;charset=UTF-8");
        }

        private JsonResult ShowError(string message)
        {
            var hash = new Hashtable();
            hash["error"] = 1;
            hash["message"] = message;
            return Json(hash, "text/html;charset=UTF-8");
        }
        private byte[] StreamToBytes(Stream stream)
        {
            var bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
        public static string getFileName()
        {
            Random rd = new Random();
            StringBuilder serial = new StringBuilder();
            serial.Append(DateTime.Now.ToString("yyMMddHHmmssff"));
            serial.Append(rd.Next(0, 9999).ToString());
            return serial.ToString();
        }

        /// <summary>
        /// 上传图片(用户头像)
        /// </summary>
        /// <returns></returns>
        public string ImgUpload()
        {

            FileUpload.FileUpload upfile = new FileUpload.FileUpload();
            HttpPostedFileBase file = Request.Files["Filedata"];
            var type = Request["type"];
            Stream stream = file.InputStream;
            byte[] b = StreamToBytes(stream);
            string img = upfile.UploadFiles(b, "users");
            return img;
        }
        /// <summary>
        /// 上传图片(商品图片)
        /// </summary>
        /// <returns></returns>
        public string ImgUploadByProducts()
        {

            FileUpload.FileUpload upfile = new FileUpload.FileUpload();
            HttpPostedFileBase file = Request.Files["Filedata"];
            var type = Request["type"];
            Stream stream = file.InputStream;
            byte[] b = StreamToBytes(stream);
            string img = upfile.UploadFiles(b, "Products");
            return img;
        }
        /// <summary>
        /// 上传图片(酒店图片)
        /// </summary>
        /// <returns></returns>
        public string ImgUploadByHotel()
        {

            FileUpload.FileUpload upfile = new FileUpload.FileUpload();
            HttpPostedFileBase file = Request.Files["Filedata"];
            var type = Request["type"];
            Stream stream = file.InputStream;
            byte[] b = StreamToBytes(stream);
            string img = upfile.UploadFiles(b, "Hotel");
            return img;
        }

        /// <summary>
        /// 上传图片(通用图片)
        /// </summary>
        /// <returns></returns>
        public string ImgUploadAll(string code)
        {

            FileUpload.FileUpload upfile = new FileUpload.FileUpload();
            HttpPostedFileBase file = Request.Files["Filedata"];
            var type = Request["type"];
            Stream stream = file.InputStream;
            byte[] b = StreamToBytes(stream);
            string img = upfile.UploadFiles(b, code);
            return img;
        }


        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        public string FileUpload()
        {
            FileUpload.FileUpload upfile = new FileUpload.FileUpload();
            HttpPostedFileBase file = Request.Files["Filedata"];
            var type = Request["type"];
            Stream stream = file.InputStream;
            byte[] b = StreamToBytes(stream);
            string img = upfile.UploadFilesByDocument(b, file.FileName);
            return img;
        }

        public string CategoryDDL(int pid)
        {
            StringBuilder sb = new StringBuilder();
            var category = Admin.Common.YTDDL.Category(s => s.Pid.Value == pid, true);
            if (category.Count() > 0)
            {
                foreach (var item in category)
                {
                    sb.AppendFormat("<option value='{0}'>{1}</option>", item.Value, item.Text);
                }
            }
            return sb.ToString();
        }
	}
}